package com.fh.area.service.Impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/12/149:07
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.area.entity.po.AreaPo;
import com.fh.area.entity.vo.Area;
import com.fh.area.entity.vo.AreaVo;
import com.fh.area.mapper.AreaMapper;
import com.fh.area.service.AreaService;
import com.fh.common.exceptions.ParamException;
import com.fh.common.exceptions.UserNoException;
import com.fh.user.entity.vo.User;
import com.fh.utils.JWT;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created by macro on 2021/12/14,
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaPo> qeuryById(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new ParamException();
        }
        User unsign = JWT.unsign(token, User.class);
        if (unsign == null) {
            throw new UserNoException("用户不存在");
        }

        return areaMapper.queryList(unsign.getId());
    }

    @Override
    public AreaVo queryAreaById(Integer id) {
        if (id == null) {
            throw new ParamException();
        }
        QueryWrapper q = new QueryWrapper();
        q.eq("id", id);
        return areaMapper.selectOne(q);
    }

    @Override
    public void upArea(AreaVo areaVo) {

        if (StringUtils.isEmpty(areaVo.getName())) {
            throw new ParamException();
        }

        if (StringUtils.isEmpty(areaVo.getIphone())) {
            throw new ParamException();
        }
        if (StringUtils.isEmpty(areaVo.getEmailCode())) {
            throw new ParamException();
        }
        areaMapper.updateById(areaVo);
    }

    @Override
    public void del(Integer id) {
        if (id == null) {
            throw new ParamException();
        }
        areaMapper.deleteById(id);
    }

    @Override
    public void add(AreaVo areaVo) {
         /*if (areaVo.getPid() == null || areaVo.getSid() == null || areaVo.getVid() == null) {
            throw new ParamException();
        }*/
        if (StringUtils.isEmpty(areaVo.getName())) {
            throw new ParamException();
        }

        if (StringUtils.isEmpty(areaVo.getIphone())) {
            throw new ParamException();
        }
        if (StringUtils.isEmpty(areaVo.getEmailCode())) {
            throw new ParamException();
        }
        areaMapper.insert(areaVo);
    }

    @Override
    public User queryToken(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new ParamException();
        }
        User unsign = JWT.unsign(token, User.class);
        User u = new User();
        u.setName(unsign.getName());
        u.setId(unsign.getId());

        return u ;
    }

    @Override
    public List<Map<String, Object>> queryArea() {
        List<Area> areaList = areaMapper.queryArea();
        return getAreaList(areaList, 0);
    }

    @Override
    public Map queryName(AreaVo areaVo) {
        Map map = new HashMap();
       String sheng= areaMapper.queryName(areaVo.getPid());
        String shi = areaMapper.queryName(areaVo.getSid());
        String xian = areaMapper.queryName(areaVo.getXid());
        map.put("sheng", sheng);
        map.put("shi", shi);
        map.put("xian", xian);
        return map;
    }

    private List<Map<String, Object>> getAreaList(List<Area> areaList, Integer pid) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Area area : areaList) {
            Map<String, Object> map = null;
            if (pid.equals(area.getPid())) {
                map = new HashMap<>();
                map.put("value", area.getId());
                map.put("label", area.getName());
                map.put("children", getAreaList(areaList, area.getId()));
                List children = (List) map.get("children");
                if (children.size() == 0) {
                    map.remove("children");
                }
            }
            if (map != null) {
                list.add(map);
            }
        }
        return list;
    }
}
